package com.wuhan.template.module.home.activity;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.wuhan.template.R;
import com.wuhan.template.base.BaseActivity;
import com.wuhan.template.base.BaseFragment;
import com.wuhan.template.module.home.adapter.JiemuDanAdapter;
import com.wuhan.template.module.home.adapter.ShiPinFragmentAdapter;
import com.wuhan.template.module.home.fragment.JIMuZhuTiFragment;
import com.wuhan.template.module.home.fragment.JieMuChatRoomFragment;
import com.wuhan.template.module.home.fragment.JieMuDanFragment;
import com.wuhan.template.utils.EventMessage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import io.vov.vitamio.MediaPlayer;
import io.vov.vitamio.widget.MediaController;
import io.vov.vitamio.widget.VideoView;

/**
 * Created by Administrator on 2016/5/12 0012.
 */
public class DianShiDateilActivity extends BaseActivity implements JiemuDanAdapter.OnVideoClickListener {

    private String url = "http://mobile.appwuhan.com/zswuhan/channel_detail.php?appid=16&appkey=rFUm5PYocCj6e1h0m03t3WarVJcMV98c&device_token=2d375c69c8348fcfa06e3cde5753339c&_member_id=&version=3.1&app_version=3.1";
    private String dianshiId;

    @BindView(R.id.dianshi_shipin)
    protected VideoView videoView;

    @BindView(R.id.shiping_tabLayout)
    protected TabLayout tabLayout;

    @BindView(R.id.shiping_detail_viewPage)
    protected ViewPager viewPager;
    private String m3u8;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_dianshi_dateil;
    }

    @Override
    protected void initViews() {

        dianshiId = getIntent().getStringExtra("dianshiId");
        m3u8 = getIntent().getStringExtra("m3u8");
        if(!videoView.isPlaying()){
            videoView.setVideoPath(m3u8);
            videoView.setMediaController(new MediaController(this));
            videoView.requestFocus();
        }


        List<Fragment> fragments = new ArrayList<>();

        Map<String, String> params = new HashMap<>();
        params.put("id", dianshiId);

        //节目单的fragment
        JieMuDanFragment jieMuDan = BaseFragment.newInstance(JieMuDanFragment.class, params);
        fragments.add(jieMuDan);
        //电视主题的的fragment
        JIMuZhuTiFragment zhuTi = BaseFragment.newInstance(JIMuZhuTiFragment.class, params);
        fragments.add(zhuTi);
        //聊天室的fragment
        JieMuChatRoomFragment chatRoom = BaseFragment.newInstance(JieMuChatRoomFragment.class, null);
        fragments.add(chatRoom);

        ShiPinFragmentAdapter fragmentAdapter = new ShiPinFragmentAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(fragmentAdapter);

        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        tabLayout.setSelectedTabIndicatorColor(Color.BLUE);
        tabLayout.setupWithViewPager(viewPager);

    }

    @Override
    protected void loadData() {

        //  OkHttpUtils.get(url + "&channel_id=" + dianshiId, null);
    }

    @Override
    protected void postEventResult(@NonNull EventMessage message) {
       /* if (message.type.equals(url + "&channel_id=" + dianshiId)) {

            Gson gson = new Gson();
            ShiPingEntity[] shiPingEntities = gson.fromJson(message.result, ShiPingEntity[].class);
            //List<ShiPingEntity> list = new ArrayList<>();
            list.addAll(Arrays.asList(shiPingEntities));
            ArrayAdapter<ShiPingEntity> arrayAdapter = new ArrayAdapter<ShiPingEntity>(this,R.layout.shiming_list_item,list);
            listView.setAdapter(arrayAdapter);
        }*/
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /*@Override
    public void onArticleSelected(String m3u8, String dianshiId) {

        videoView.setVideoPath(m3u8);
        videoView.setMediaController(new MediaController(this));
        videoView.requestFocus();

        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mediaPlayer.setPlaybackSpeed(1.0f);
            }
        });

    }*/

    @Override
    public void onVideoItemClick(View view, String m3u8, String dianshiId) {
        videoView.setVideoPath(m3u8);
        videoView.setMediaController(new MediaController(this));
        videoView.requestFocus();

        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mediaPlayer.setPlaybackSpeed(1.0f);
            }
        });

    }
}
